﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BussinessLayer;

namespace projectQLDV
{
    public partial class frmChuyenSinhHoat : DevExpress.XtraEditors.XtraForm
    {
        private frmMain formM;
        private int[] idDV;
        private string namhoc;
        public frmChuyenSinhHoat(frmMain frm, int[] id, string nh)
        {
            InitializeComponent();
            formM = frm;
            idDV = id;
            namhoc = nh;
            loadDonVi(false);
        }

        private void loadDonVi(bool b)
        {
            this.comboBoxDonVi.Properties.Items.Clear();
            if (b)
            {
                tblDoanvien_chuyensinhhoat dvcsh = new tblDoanvien_chuyensinhhoat();
                DataTable dt = new DataTable();
                dt = dvcsh.SelectDonViChuyenDi();
                foreach (DataRow r in dt.Rows)
                {
                    this.comboBoxDonVi.Properties.Items.Add(r["donvi"].ToString());
                }
            }
            else
            {
                tblchidoan cd = new tblchidoan();
                DataTable dt = new DataTable();
                dt = cd.SelectCDTheoNamHoc(this.namhoc);
                foreach (DataRow r in dt.Rows)
                {
                    this.comboBoxDonVi.Properties.Items.Add(r["khoi"].ToString() + r["lop"].ToString());
                }
            }
        }

        private void checkTruong_CheckedChanged(object sender, EventArgs e)
        {
            loadDonVi(!this.checkTruong.Checked);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            tblDoanvien_chuyensinhhoat dvcsh = new tblDoanvien_chuyensinhhoat();
            try
            {
                foreach (int i in this.idDV)
                {
                    dvcsh.Insert(i, DateTime.Parse(this.dateEditNgayChuyen.Text).Date, this.comboBoxDonVi.Text, this.checkDonViKhac.Checked);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void frmChuyenSinhHoat_FormClosed(object sender, FormClosedEventArgs e)
        {
            formM.frmChuyenSinhHoatDoan_isShown = false;
        }
    }
}